capture program drop construct_base_4decile
program define construct_base_4decile
* Arguments
* 1: input
* 2: output
* 3 4: left and right censoring
* 5: either whole for whole sample or sub for sub-periods 
* 6: country group to be considered
* summary statistics on durations are computed for the whole period only
clear 
g $product=0 
drop if $product==0 
save `2', replace 

local j=`j'+3

foreach i in `6' {
	use `1', clear
	keep if iso2=="`i'"
	save transaction_`i', replace

	use transaction_`i', clear
	tab iso2 
	drop if $product==0
	sort year month	
	id_group id_t "year month" // id_group does the same job as egen group (but work if we have millions of groups)
	sort iso2 buyer $product year month siren
	id_group id_sc "siren iso2" // id_group does the same job as egen group (but work if we have millions of groups)
	id_group id_b "iso2 buyer" // id_group does the same job as egen group (but work if we have millions of groups)
	id_group id_bp "iso2 buyer $product" // id_group does the same job as egen group (but work if we have millions of groups)
	id_group id_bst "id_bp id_t id_s" // id_group does the same job as egen group (but work if we have millions of groups)
	id_group id_bs "id_bp id_s"	 // id_group does the same job as egen group (but work if we have millions of groups)
	sort id_bp id_t
	id_group _ "id_bp id_t"
	by id_bp: egen __=min(_)
	g id_t_bp=_-__+1
	drop _ __
	sort id_sc id_bp id_t_bp
	g switch=0
	by id_sc id_bp: replace switch=1 if _n==1
	* switch =1 the first time the seller and the buyer ever interact (including the first time the buyer appears in the sample) 
	sort id_sc id_bp id_t_bp
	g recall=0
	replace recall=1 if id_t_bp!=id_t_bp[_n-1]+1 & id_sc==id_sc[_n-1] & id_bp==id_bp[_n-1] 
	* recall=1 for a switch towards a seller the buyer had already met
	sort id_bp id_sc id_t_bp
	by id_bp id_sc: g _=sum(recall)
	id_group id_bs_continuous "id_bp id_sc _"
	*  id_bs_continuous identifies seller buyer continuous relationships (ie recalls are attributed a different id)
	drop _
	* length of the buyer's history
	sort id_bp id_t
	by id_bp: g first=1 if _n==1
	by id_bp: g last=1 if _n==_N
	g date=year+(month-1)/12
	g _=date*first
	by id_bp: egen datefirst=mean(_)
	drop _
	g _=date*last	
	by id_bp: egen datelast=mean(_)	
	drop _
	g length_bp=(datelast-datefirst)*12+1
	drop datelast datefirst first last
	// length_bp defined as the difference between the first and the last months of a buyer's history
	* length of a continuous relationship 
	sort id_bs_continuous id_t
	by id_bs_continuous: g first=1 if _n==1
	by id_bs_continuous: g last=1 if _n==_N
	g _=date*first
	by id_bs_continuous: egen datefirst=mean(_)
	drop _
	g _=date*last	
	by id_bs_continuous: egen datelast=mean(_)
	drop _
	g length_bps=(datelast-datefirst)*12+1
	// length_bps defined as the difference between the first and the last months of a continuous relationship

	

	g firstyear_sample=floor(datefirst)
	`3' // drop all seller*buyer relationships that we do not observe from the start (treats left-censoring)
	`4' // drop all seller*buyer relationships starting two years before the end (treats right-censoring)
	
	drop datelast datefirst first last firstyear_sample // firstyear_panel
	by id_bs_continuous: egen num_transac=count(export)	 
	bys id_bp: egen count_transactions_bp=nvals(id_bst)
	bys id_bp: egen count_sellers_bp=nvals(id_sc)
	*  count_sellers_bp is the number of different sellers (recalls counted just once)
	bys id_bp: egen count_continuous_bp=nvals(id_bs_continuous)
	*  count_continuous_bp is the number of relationships (recalls counted separately)
	bys id_bp: egen count_switch_norecall_bp=sum(switch)
	bys id_bp: egen count_recall_bp=sum(recall)
	g count_switch_bp=count_switch_norecall_bp+count_recall_bp
	*  count_switch_norecall_bp is the number of switches towards an unknown seller
	*  count_retake_bp is the number of recalls
	*  count_switch_bp is the total number of switches
	g proba_switch_bp=count_switch_bp/(count_transactions_bp)
	g proba_switch_unconditional_bp=count_switch_bp/length_bp
	g proba_recall_bp=count_recall_bp/count_switch_bp
	g frequency_bp=count_transactions_bp/length_bp
	compress
	collapse (mean) num_transac length_bp length_bps count_transactions_bp count_sellers_bp count_continuous_bp count_switch_bp count_recall_bp proba_switch_bp proba_switch_unconditional_bp proba_recall_bp frequency_bp export (sum) totx=export, by(id_bs_continuous id_bs id_bp $product iso2)
	* collapse across transactions within a (continuous) buyer* seller* $product
	append using `2'
	save `2', replace 
	erase transaction_`i'.dta
}
g _="`5'"
if _=="whole"{
	global endcountry=`j'
}
end
